//
//  ZXTextView.m
//  qianke
//
//  Created by ZX on 2017/7/13.
//  Copyright © 2017年 ZX. All rights reserved.
//

#import "ZXTextView.h"

@implementation ZXTextView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        self.placeHolderColor = [UIColor grayColor];
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChange) name:UITextViewTextDidChangeNotification object:self];
    }
    
    return self;
}

- (void)textDidChange
{
    [self setNeedsDisplay];
}

- (void)setFont:(UIFont *)font
{
    [super setFont:font];
    [self setNeedsDisplay];
}

- (void)setPlaceHolder:(NSString *)placeHolder
{
    _placeHolder = placeHolder;
    [self setNeedsDisplay];
}

- (void)setPlaceHolderColor:(UIColor *)placeHolderColor
{
    _placeHolderColor = placeHolderColor;
    [self setNeedsDisplay];
}

- (void)drawRect:(CGRect)rect
{
    if (self.hasText) {
        return;
    }
    
    CGFloat placeX = 5;
    CGFloat placeY = 8;
    CGFloat placeW = rect.size.width - 2 * placeX;
    CGRect placeRect = CGRectMake(placeX, placeY, placeW, rect.size.height);
    
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    dict[NSForegroundColorAttributeName] = self.placeHolderColor;
    dict[NSFontAttributeName] = self.font;
    
    [self.placeHolder drawInRect:placeRect withAttributes:dict];
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}



@end
